class RollsController < ApplicationController
  
  before_filter :authorize
  
  layout :determine_layout
  
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @fabric = Fabric.find(params[:id]) unless !params[:id]
    if @fabric
      @roll_pages, @rolls = paginate :rolls, :per_page => 10, :conditions => "fabric_id = #{@fabric.id}"
    else
      @roll_pages, @rolls = paginate :rolls, :per_page => 10
    end
  end

  def show
    @roll = Roll.find(params[:id])
  end

  def new
    @roll = Roll.new
  end

  def create
    @roll = Roll.new(params[:roll])
    
    if @roll.save
      flash[:notice] = 'Roll was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @roll = Roll.find(params[:id])
  end

  def update
    @roll = Roll.find(params[:id])
    if @roll.update_attributes(params[:roll])
      flash[:notice] = 'Roll was successfully updated.'
      redirect_to :action => 'show', :id => @roll
    else
      render :action => 'edit'
    end
  end

  def destroy
    Roll.find(params[:id]).destroy
    redirect_to :action => 'list'
  end

  # Doff reltated actions
  def doff_list
    @packed = RollState.find_by_code(5)
    @user = User.find(session[:user_id])
    @roll_pages, @rolls = paginate  :rolls, :per_page => 10, 
                                    :conditions => ["doffed_by_id = ? and roll_state_id != ?", 
                                                    session[:user_id], @packed.id ]
  end    
  
  def doff
    @roll = Roll.find(params[:id])
  end
  
  def add_doff
    @roll = Roll.find(params[:id])
    temp_roll = Roll.new(params[:roll])
    @roll.length = temp_roll.length
    @roll.packaging_type = temp_roll.packaging_type
    @roll.doffer = User.find(session[:user_id])
    @roll.doff_date = Time.now
    @roll.roll_state = RollState.find_by_code(4) # Set state to 'Inventory'
    
    if @roll.calulate_weights
      if @roll.save
        flash[:notice] = 'Roll was successfully updated with a pkg type of ' + @roll.packaging_type.name
        redirect_to :action => 'defect', :id => @roll
      else
        flash[:notice] = 'There was a problem updating the roll.'
        redirect_to :action => 'doff', :id => @roll
      end
    else
      redirect_to :action => 'doff', :id => @roll
    end
   end
   
   def complete_doff
     @roll = Roll.find(params[:id])
     @roll.roll_state = RollState.find_by_code(5)
     
     if @roll.save
       flash[:notice] = "Roll processing completed."
     else
       flash[:notice] = 'There was a problem updating the roll.'
     end
     redirect_to :action => 'list'
   end
   
   # Load related actions
   def list_loads
      @roll = Roll.find(params[:id])
      @loom_load_pages, @loom_loads = paginate :loom_loads, :conditions => [ "roll_id = ?", @roll.id ], :per_page => 10
    end
   
   # defect related actions
   def defect
     @roll = Roll.find(params[:id])
     @roll_defect = RollDefect.new
   end
   
   def list_defects
     @roll = Roll.find(params[:id])
     @roll_defect_pages, @roll_defects = paginate :roll_defects, :conditions => [ "roll_id = ?", @roll.id ], :per_page => 10
   end
   
   def add_defect
     @roll = Roll.find(params[:id])
     @roll_defect = RollDefect.new(params[:roll_defect])
     @roll.roll_defects.push(@roll_defect)
     if @roll.save
       flash[:notice] = 'Defect was successfull added.' 
       redirect_to :action => 'list_defects', :id => @roll
     else
       flash[:notice] = 'There was a problem updating the roll.'
       redirect_to :action => 'defect', :id => @roll
     end
    end
    
    def edit_defect
      @roll_defect = RollDefect.find(params[:id])
    end

    def update_defect
      @roll_defect = RollDefect.find(params[:id])
      if @roll_defect.update_attributes(params[:roll_defect])
        flash[:notice] = 'Roll Defect was successfully updated.'
        redirect_to :action => 'list_defects', :id => @roll_defect.roll
      else
        flash[:notice] = 'There was a problem updating the roll defect.'
        render :action => 'edit_defect'
      end
    end

    def destroy_defect
      @roll_defect = RollDefect.find(params[:id])
      temp_roll = @roll_defect.roll 
      @roll_defect.destroy
      redirect_to :action => 'show', :id => temp_roll
    end
    
    
    # The printing functions
    def print_slip
      @roll = Roll.find(params[:id])
    end
      
end
